package com.hfzy.ihk.web.freeswitchManage.websocket.swx.base;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;

import java.io.IOException;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/2.
 *
 * @author wws
 */
public abstract class AbstractBrowserWebSocketHandler extends AbstractWebSocketHandler {

    private static final Logger logger = LoggerFactory.getLogger(AbstractBrowserWebSocketHandler.class);

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {

        Map<String,Object> attributes = session.getAttributes();

        String userId = (String)attributes.get("userId");
        String token = (String)attributes.get("token");
        String machineId = (String)attributes.get("machineId");

        logger.info("handleTransportError: userId:{},machineId:{},token:{}, message:{}",userId,machineId,token,exception.getMessage());

        if(session.isOpen()) {
            try {
                session.close();
            } catch (IOException e) {
                logger.error("close webSocket failed: userId:{},machineId:{},token:{}, message:{}",userId,machineId,token,exception.getMessage());
            }
        }
    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }
}
